package com.cyanite.playlv2api.parsers.json;

import com.cyanite.playlv2api.types.Trip;

import org.json.JSONException;
import org.json.JSONObject;

public class TripParser extends AbstractParser<Trip> {

    @SuppressWarnings("unchecked")
    @Override
    public Trip parse(JSONObject json) throws JSONException {

        Trip obj = new Trip();
        if (json.has("tid")) {
            obj.setId(String.valueOf(json.getInt("tid")));
        }
        if (json.has("uid")) {
            obj.setUserId(String.valueOf(json.getInt("uid")));
        }
        if (json.has("nick")) {
            obj.setUserName(json.getString("nick"));
        }
        if (json.has("avatar")) {
            obj.setUserPhoto(json.getString("avatar"));
        }
        if (json.has("name")) {
            obj.setName(json.getString("name"));
        }
        if (json.has("sp")) {
            obj.setStartpoint(json.getString("sp"));
        }
        if (json.has("ep")) {
            obj.setEndpoint(json.getString("ep"));
        }
        if (json.has("name")) {
            obj.setName(json.getString("name"));
        }
        if (json.has("kid"))   {
            obj.setCategoryId(json.getInt("kid")-1);
        }
        if (json.has("photo")) {
            obj.setPhoto(json.getString("photo"));
        }
        if (json.has("focus")) {
            obj.setFocusedCount(json.getInt("focus"));
        }
        if (json.has("checkin")) {
            obj.setCheckinsCount(json.getInt("checkin"));
        }
        if (json.has("process")) {
            obj.setProcess(json.getInt("process"));
        }
        if (json.has("fstatus"))  {
            if (json.getInt("fstatus") == 1)  obj.setFocused(true);
            else    obj.setFocused(false);
        }
        if (json.has("scount")) {
            obj.setTasksCount(json.getInt("scount"));
        }
        if (json.has("update")) {
            obj.setUpdateTime(json.getLong("update"));
        }
        if (json.has("date")) {
            obj.setStartDate(json.getString("date"));
        }
        if (json.has("days")) {
            obj.setTotalDays(json.getString("days"));
        }
        if (json.has("users")) {
            obj.setFriendsInCommon(
                new GroupParser(
                    new UserParser()).parse(json.getJSONArray("users")));
        }
        if (json.has("trips")) {
            obj.setTripsRecommend(
                new GroupParser(
                    new TripMiniParser()).parse(json.getJSONArray("trips")));
        }
        return obj;
    }
}